package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
)

// CreateSecret 创建secret
// @Summary 创建secret
// @Description 创建secret
// @Tags Secret
// @Accept  application/json
// @Product application/json
// @Param data body service.CreateDockerSecreteRequest true "创建secret的信息"
// @Success 200 {object} response.Response{data=v1.Secret} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/secret [post]
// @Security Bearer
func CreateSecret(c *gin.Context) {
	var request service.CreateDockerSecreteRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	secretInfo, err := service.CreateSecret(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, secretInfo)
}

// GetSecretList 查看secret列表
// @Summary 查看secret列表
// @Description 查看secret列表
// @Tags Secret
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.SecretList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/secret/list [get]
// @Security Bearer
func GetSecretList(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	secretList, err := service.GetSecretList(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, secretList)
}

// GetSecret 查看指定secret
// @Summary 查看指定secret
// @Description 查看指定secret
// @Tags Secret
// @Param namespace_name query string true "namespace名字"
// @Param secret_name query string true "secret的名字"
// @Success 200 {object} response.Response{data=v1.Secret} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/secret [get]
// @Security Bearer
func GetSecret(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	secretName := c.Query("secret_name")
	secretInfo, err := service.GetSecret(config.K8sClientSet, namespaceName, secretName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, secretInfo)
}

// ApplySecret 修改secret
// @Summary 修改secret
// @Description 修改secret
// @Tags Secret
// @Accept  application/json
// @Product application/json
// @Param data body service.ApplySecretRequest true "修改secret的信息"
// @Success 200 {object} response.Response{data=v1.Secret} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/secret [put]
// @Security Bearer
func ApplySecret(c *gin.Context) {
	var request service.ApplySecretRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	secretInfo, err := service.ApplySecret(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, secretInfo)
}

// DeleteSecret 删除指定secret
// @Summary 删除指定secret
// @Description 删除指定secret
// @Tags Secret
// @Accept  application/json
// @Product application/json
// @Param data body service.DeleteSecretRequest true "删除secret的信息"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/secret [delete]
// @Security Bearer
func DeleteSecret(c *gin.Context) {
	var request service.DeleteSecretRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	err = service.DeleteSecret(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}
